package com.wz.datachannel.util;

import android.os.Looper;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.BinaryHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.SyncHttpClient;

/**
 * android-async-http工具类
 * 
 */
public class HttpClientUtil {
	private static AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); // 实例话对象，异步
	private static AsyncHttpClient syncHttpClient = new SyncHttpClient(); // 实例话对象，同步

	static {
		asyncHttpClient.setTimeout(30 * 1000);
		syncHttpClient.setTimeout(30 * 1000); // 设置链接超时，如果不设置，默认为0s

		asyncHttpClient.setURLEncodingEnabled(true);
		syncHttpClient.setURLEncodingEnabled(true);

	}

	/**
	 * 用一个完整url获取一个string对象(post)
	 * 
	 * @param urlString
	 * @param res
	 */
	public static void post(String urlString, AsyncHttpResponseHandler res) {
		getClient().post(urlString, res);

	}

	/**
	 * url里面带参数(post)
	 * 
	 * @param urlString
	 * @param params
	 * @param res
	 */
	public static void post(String urlString, RequestParams params, AsyncHttpResponseHandler res) {
		getClient().post(urlString, params, res);

	}

	/**
	 * 不带参数，获取json对象或者数组(post)
	 * 
	 * @param urlString
	 * @param res
	 */
	public static void post(String urlString, JsonHttpResponseHandler res) {
		getClient().post(urlString, res);
	}

	/**
	 * 带参数，获取json对象或者数组(post)
	 * 
	 * @param urlString
	 * @param params
	 * @param res
	 */
	public static void post(String urlString, RequestParams params, JsonHttpResponseHandler res) {
		getClient().post(urlString, params, res);
	}

	/**
	 * 下载数据使用，会返回byte数据(post)
	 * 
	 * @param uString
	 * @param bHandler
	 */
	public static void post(String uString, BinaryHttpResponseHandler bHandler) {
		getClient().post(uString, bHandler);
	}

	/**
	 * 用一个完整url获取一个string对象(get)
	 * 
	 * @param urlString
	 * @param res
	 */
	public static void get(String urlString, AsyncHttpResponseHandler res) {
		getClient().get(urlString, res);
	}

	/**
	 * url里面带参数(get)
	 * 
	 * @param urlString
	 * @param params
	 * @param res
	 */
	public static void get(String urlString, RequestParams params, AsyncHttpResponseHandler res) {
		getClient().get(urlString, params, res);
	}

	/**
	 * 不带参数，获取json对象或者数组(get)
	 * 
	 * @param urlString
	 * @param res
	 */
	public static void get(String urlString, JsonHttpResponseHandler res) {
		getClient().get(urlString, res);
	}

	/**
	 * 带参数，获取json对象或者数组(get)
	 * 
	 * @param urlString
	 * @param params
	 * @param res
	 */
	public static void get(String urlString, RequestParams params, JsonHttpResponseHandler res) {
		getClient().get(urlString, params, res);
	}

	/**
	 * 下载数据使用，会返回byte数据(get)
	 * 
	 * @param uString
	 * @param bHandler
	 */
	public static void get(String uString, BinaryHttpResponseHandler bHandler) {
		getClient().get(uString, bHandler);
	}

	public static AsyncHttpClient getClient() {
		if (Looper.myLooper() == null)
			return syncHttpClient;
		return asyncHttpClient;
	}

	public static AsyncHttpClient getClient(boolean isSync) {
		if (isSync) {
			return syncHttpClient;
		} else {
			return asyncHttpClient;
		}
	}

}